<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { getAreaInfoApi } from '@/api/warehouse/warehouseArea'
import { getInfoApi } from '@/api/warehouse/warehouse'
import { addApi } from '@/api/warehouse/warehouseAreaLocal'
const dialogVisible = ref(false)
const dialogFormData = ref({
  warehouseName: null,
  warehouseArea: null,
  category: null,
  locaCode: null,
  locaLen: null,
  locaWidth: null,
  locaHeight: null,
  localVolume: null,
  localBearingCapaity: null,
  tunnelNumber: null,
  shelfNumber: null,
  layerNumber: null,
})
const resetForm = () => {
  dialogVisible.value = false
  dialogFormData.value = {
    warehouseName: null,
    warehouseId: null,
    warehouseArea: null,
    warehouseAreaId: null,
    category: null,
    locaCode: null,
    locaLen: null,
    locaWidth: null,
    locaHeight: null,
    localVolume: null,
    localBearingCapacity: null,
    tunnelNumber: null,
    shelfNumber: null,
    layerNumber: null,
    isDel: '1'
  }
}
const confirm = async () => {
  let res = null;
  if (dialogFormData.value.id) {
    res = await updateApi(dialogFormData.value)
  } else {
    res = await addApi(dialogFormData.value)
  }
  if (res.code) {
    ElMessage.success('操作成功!')
    resetForm()
    instance.emit('search')
  } else {
    ElMessage.error('操作失败！')
  }
}

const WarehouseList = ref(null)
const WarehouseAreaList = ref(null)
const getWarehouseList = async () => {
  let res = await getInfoApi()
  WarehouseList.value = res
}
const getWarehouseAreaList = async () => {
  let res = await getAreaInfoApi()
  WarehouseAreaList.value = res
}
// 处理库区选择变化
const handleWarehouseAreaChange = (val) => {
  // 在 WarehouseAreaList 中查找选中的库区对象
  const selectedArea = WarehouseAreaList.value?.find(area => area.id === val)
  if (selectedArea) {
    // 设置库区类型
    dialogFormData.value.category = selectedArea.category
    dialogFormData.value.warehouseAreaId = selectedArea.id
  } else {
    // 如果没有选中或清空选择，重置 category
    dialogFormData.value.category = null
    dialogFormData.value.warehouseAreaId = null
  }
}
// 处理库区选择变化
const handleWarehouseChange = (val) => {
  // 在 WarehouseAreaList 中查找选中的库区对象
  const selectedArea = WarehouseList.value?.find(area => area.name === val)
  if (selectedArea) {
    // 设置库区类型
    dialogFormData.value.warehouseId = selectedArea.id
  } else {
    // 如果没有选中或清空选择，重置 category
    dialogFormData.value.warehouseId = null
  }
}
defineExpose({
  dialogFormData,
  dialogVisible,
  getWarehouse: getWarehouseList,
  getWarehouseArea: getWarehouseAreaList
})
</script>
<template>
  <el-dialog v-model="dialogVisible" style="width: 400px;">
    <el-form ref="ruleFormRef" :model="dialogFormData" :rules="rules">
      <el-row>
        <el-col span="24">
          <el-select v-model="dialogFormData.warehouseName" placeholder="仓库名称" style="width: 300px;"
            @change="handleWarehouseChange">
            <el-option v-for="warehouse in WarehouseList" :label="warehouse.name" :value="warehouse.name" />
          </el-select>
        </el-col>
      </el-row>
      <br />
      <el-row>
        <el-col span="24">
          <el-select v-model="dialogFormData.warehouseArea" placeholder="库区名称" style="width: 300px;"
            @change="handleWarehouseAreaChange">
            <el-option v-for="warehouseArea in WarehouseAreaList" :label="warehouseArea.name"
              :value="warehouseArea.id" />
          </el-select>
        </el-col>
      </el-row>
      <br />
      <el-row>
        <el-col span="24">
          <el-form-item prop="">
            <el-input v-model="dialogFormData.category" placeholder="库区类型" style="width: 300px;" disabled />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col span="24">
          <el-form-item prop="">
            <el-input v-model="dialogFormData.locaCode" placeholder="库区编码" style="width: 300px;" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col span="24">
          <el-form-item prop="">
            <el-input v-model="dialogFormData.locaLen" placeholder="库区长度" style="width: 300px;" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col span="24">
          <el-form-item prop="">
            <el-input v-model="dialogFormData.locaWidth" placeholder="库区宽度" style="width: 300px;" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col span="24">
          <el-form-item prop="">
            <el-input v-model="dialogFormData.locaHeight" placeholder="库区高度" style="width: 300px;" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col span="24">
          <el-form-item prop="">
            <el-input v-model="dialogFormData.localVolume" placeholder="库区容积" style="width: 300px;" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col span="24">
          <el-form-item prop="">
            <el-input v-model="dialogFormData.localBearingCapacity" placeholder="库区承重" style="width: 300px;" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col span="24">
          <el-form-item prop="">
            <el-input v-model="dialogFormData.tunnelNumber" placeholder="巷道号" style="width: 300px;" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col span="24">
          <el-form-item prop="">
            <el-input v-model="dialogFormData.shelfNumber" placeholder="货架号" style="width: 300px;" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col span="24">
          <el-form-item prop="">
            <el-input v-model="dialogFormData.layerNumber" placeholder="层号" style="width: 300px;" />
            <el-form-item prop="" label="是否生效">
              <el-switch v-model="dialogFormData.isDel" active-value='1' inactive-value='0' />
            </el-form-item>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="resetForm">取消</el-button>
        <el-button type="primary" @click="confirm">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>